/**
 * Created by wengms on 2016/11/20.
 */
define([angular], function () {
    return function($scope,GillionMsg,Resource,$dataSourceManager,$rootScope){
        $scope.project={};
        $scope.removeShow=false;
        var Projects = Resource("/grule-manager/projects/:id",{
            "id":"@id"
        });
        $scope.actions = {
            openEditView:function(project){
                var title = !project?"新建项目":"项目编辑";
                GillionMsg.showUrl({
                    title:title,
                    data:project,
                    url: '/html2/system/project/project-edit',
                    width:400,
                    height:360,
                    onClose:function(data){
                        //刷新表格内容
                        $scope.actions.refreshTable();
                    }
                });
            },
            openChildView:function(id){
                var project ={};
                project.parentId=id;
                GillionMsg.showUrl({
                    title:"新增子项目",
                    data:project,
                    url: '/html2/system/project/project-child-add',
                    width:400,
                    height:360,
                    onClose:function(data){
                        //刷新表格内容
                        $scope.actions.refreshTable();
                    }
                });
            },
            deleteProject:function(id){
                GillionMsg.confirm('提示信息', '是否确定删除？', function (r) {
                    if (r) {
                        Projects.delete({"id": id}, function (response) {
                            if (response.success) {
                                GillionMsg.alert("提示", "项目删除成功");
                                $scope.actions.refreshTable();
                            } else {
                                GillionMsg.alert("提示", "项目删除失败");
                                $scope.actions.refreshTable();
                            }
                        });
                    }
                })
            },
            search:function(){
                $dataSourceManager.dataSources['ProjectDataSource'].doRequestData(1,$scope.project);
            },
            refreshTable:function(){
                $dataSourceManager.dataSources['ProjectDataSource'].doRequestData();
            },
            authority:function(id,mode){
                GillionMsg.showUrl({
                    title:"授权管理",
                    data:{"targetId":id,"targetType":0,"mode":mode},
                    url: '/html2/system/permission/permissions-manager',
                    width:400,
                    height:400
                });
            }
        };
        $scope.myKeyup = function(e){
            var keycode = window.event?e.keyCode:e.which;
            if(keycode==13){
                $scope.actions.search();
            }
        };
        $scope.$watch('project.projectName',function(newValue,oldValue){
            if(newValue==""){
                $scope.removeShow=false;
                $scope.actions.search();
            }else if(newValue){
                $scope.removeShow=true;
            }
        });
        $scope.deleteInput=function(){
            $scope.project={projectName:""};
        }
    };
});